package com.wj.service.impl;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hospital.common.base.PageResult;
import com.hospital.common.dto.upload.module.DeleteFilesContactDto;
import com.hospital.common.dto.upload.module.FindOnePostWithResourceContactDto;
import com.hospital.common.dto.upload.module.FindPostWithResourceCountDto;
import com.hospital.common.dto.upload.module.SavePicturePathDto;
import com.hospital.common.pojo.TResource;
import com.hospital.common.utils.TakePageNum;
import com.hospital.common.vo.upload.module.FindOneContentWithResourceVo;
import com.hospital.common.vo.upload.module.FindOnePostWithResourceContactVo;
import com.hospital.common.vo.upload.module.FindPostWithResourceCountVo;
import com.wj.mapper.TResourceMapper;
import com.wj.service.ITResourceService;
import com.wj.service.MinioService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 资源映射表 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-09-26
 */
@Service
public class TResourceServiceImpl extends ServiceImpl<TResourceMapper, TResource> implements ITResourceService {
    @Autowired
    private TResourceMapper tResourceMapper;
    @Autowired
    private MinioService minioService;

    @Override
    public Integer savePicturePath(SavePicturePathDto savePicturePathDto) {
        if(savePicturePathDto.getJsonDeletePathList() != null&& savePicturePathDto.getJsonDeletePathList().length() > 0){
            minioService.deleteFiles(savePicturePathDto.getJsonDeletePathList());
        }

        return tResourceMapper.savePicturePath(savePicturePathDto);
    }

    @Override
    public Integer deleteFilesContact(DeleteFilesContactDto dto) {
        TResource oneContact = tResourceMapper.findOneContact(dto);
        if (oneContact != null) {
            minioService.deleteFiles(oneContact.getAttachmentPath());
        }
        return tResourceMapper.deleteFilesContact(dto);
    }

    @Override
    public PageResult findPostWithResourceCount(FindPostWithResourceCountDto findPostWithResourceCountDto) {
        findPostWithResourceCountDto.setPageNum(TakePageNum.getPageNum
                (findPostWithResourceCountDto.getPageNum(),
                        findPostWithResourceCountDto.getPageSize()));
        List<FindPostWithResourceCountVo> findPostWithResourceCountVoList =
                tResourceMapper.findPostWithResourceCount(findPostWithResourceCountDto);
        for (FindPostWithResourceCountVo countVo : findPostWithResourceCountVoList) {
            String attachmentPath = countVo.getAttachmentPath();
            List<String> pictures = new ArrayList<>();
//            List<String> attachmentPathList = (List<String>)JSONUtil.parse(attachmentPath);
            // 关键：按JSON数组解析（而非通用解析）
            JSONArray jsonArray = JSONUtil.parseArray(attachmentPath);

            // 转成List<String>（类型安全）
            List<String> attachmentPathList = jsonArray.toList(String.class);
            for (String attachment : attachmentPathList) {
                pictures.add(minioService.getFileUrl( attachment));
            }
            countVo.setPictures(pictures);
        }
        Long total = tResourceMapper.findPostWithResourceCountTotal(findPostWithResourceCountDto);
        PageResult<FindPostWithResourceCountVo> pageResult = new PageResult<>(total, findPostWithResourceCountVoList);
        return pageResult;
    }

    @Override
    public PageResult findOnePostWithResourceContact(FindOnePostWithResourceContactDto findOnePostWithResourceContactDto) {
        findOnePostWithResourceContactDto.setPageNum(TakePageNum.getPageNum(
                findOnePostWithResourceContactDto.getPageNum(),
                findOnePostWithResourceContactDto.getPageSize()
        ));
        List<FindOnePostWithResourceContactVo> findOnePostWithResourceContactVoList =
                tResourceMapper.findOnePostWithResourceContact(findOnePostWithResourceContactDto);
        for (FindOnePostWithResourceContactVo countVo : findOnePostWithResourceContactVoList) {
            String attachmentPath = countVo.getAttachmentPath();
            List<String> pictures = new ArrayList<>();
            Object result1 = JSONUtil.parse(attachmentPath);
            System.out.println("第一次解析结果类型: " + result1.getClass().getName());
            // 关键：按JSON数组解析（而非通用解析）
            JSONArray jsonArray = JSONUtil.parseArray(attachmentPath);

            // 转成List<String>（类型安全）
            List<String> attachmentPathList = jsonArray.toList(String.class);
            for (String attachment : attachmentPathList) {

                String fileUrl = minioService.getFileUrl(attachment);
                System.out.println("fileUrl = " + fileUrl);
                pictures.add(fileUrl);
            }
            countVo.setPictures(pictures);
        }
        Long total = tResourceMapper.findOnePostWithResourceContactTotal(findOnePostWithResourceContactDto);
        PageResult<FindOnePostWithResourceContactVo> pageResult = new PageResult<>(total, findOnePostWithResourceContactVoList);
        return pageResult;
    }

    @Override
    public FindOneContentWithResourceVo findOneContentWithResource(Long contentId) {
        FindOneContentWithResourceVo oneContentWithResource = tResourceMapper.findOneContentWithResource(contentId);
        System.out.println("oneContentWithResource = " + oneContentWithResource);
        // 关键：按JSON数组解析（而非通用解析）
        JSONArray jsonArray = JSONUtil.parseArray(oneContentWithResource.getAttachmentPath());

        // 转成List<String>（类型安全）
        List<String> stringList = jsonArray.toList(String.class);
        List<String> pictures = new ArrayList<>();
        for (String puth : stringList) {
            String fileUrl = minioService.getFileUrl(puth);
            System.out.println("fileUrl = " + fileUrl);
            pictures.add(fileUrl);
        }
        oneContentWithResource.setPictures
                (pictures);

        return oneContentWithResource;
    }

}
